package com.altio.swing.portlet;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.UIManager;

public class PortletPanel extends JComponent {
  
  private String caption = "";
  
  private Component contentPane;
  
  private List<CloseListener> closeListeners = new ArrayList<CloseListener>();
  
  private CloseOperation closeOperation = CloseOperation.HIDE;

  private int restoreHeight;
  
  private boolean closable = true;
  
  public static enum CloseOperation {
    HIDE,
    REMOVE,
    NOTHING
  }
  
  protected Component getContentPane() {
    return contentPane;
  }


  /**
   * The UI class ID string.
   */
  public static final String uiClassID = "PortletPanelUI";

  public PortletPanel(Component contentPane) {
    super();
    this.contentPane = contentPane;
    
    updateUI();
    add(contentPane, BorderLayout.CENTER);
  }
  
  /*
   * (non-Javadoc)
   * 
   * @see javax.swing.JComponent#updateUI()
   */
  @Override
  public void updateUI() {
    if (UIManager.get(getUIClassID()) != null) {
      setUI(UIManager.getUI(this));
    } else {
      setUI(PortletPanelUI.createUI(this));
    }
  }

  
  @Override
  public String getUIClassID() {
    return uiClassID;
  }

  public void close() {
    
    for(CloseListener listener : closeListeners) {
     listener.panelClosing(this);
    }
    
    JComponent parent = null;
    if(getParent() instanceof JComponent) {
      parent = ((JComponent) getParent());
    }
    
    switch (closeOperation) {
    
      case HIDE: {
        setVisible(false);
        break;
      }
      
      case REMOVE: {
        getParent().remove(this);
        break;
      }
      
      default: {
        // do nothing
        break;
      }
    }
    
    if(parent != null) {
      parent.revalidate();
    }
    
    
    for(CloseListener listener : closeListeners) {
      listener.panelClosed(this);
     }
  }
  
  public void setMinimized(final boolean minimized) {
    if(minimized) {
      restoreHeight = getHeight();
      setSize(getWidth(), getBorder().getBorderInsets(this).top + getBorder().getBorderInsets(this).bottom);
    } else {
      setSize(getWidth(), restoreHeight);      
    }
    
    if(getParent() instanceof JComponent) {
      ((JComponent) getParent()).revalidate();
    }
  }
  
  public boolean isMinimized() {
    return getHeight() < restoreHeight;
  }
  
  public interface CloseListener {
   public void panelClosing(PortletPanel panel);
   public void panelClosed(PortletPanel panel);
  }
  
  public boolean addCloseListener(CloseListener listener) {
    return closeListeners.add(listener);
  }

  public boolean removeCloseListener(CloseListener listener) {
    return closeListeners.remove(listener);
  }
  
  public CloseOperation getCloseOperation() {
    return closeOperation;
  }

  public void setCloseOperation(final CloseOperation closeOperation) {
    this.closeOperation = closeOperation;
  }

  public void setClosable(boolean closable) {
    if(closable != this.closable) {
      this.closable = closable;
      
      // TODO add property listener so ui can decide whether revalidation is necessary?
      revalidate();
    }
  }

  public boolean isClosable() {
    return closable;
  }
  
  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }
}
